<?php

namespace app\api\controller\zi;

use addons\shopro\library\PayService;
use addons\shopro\library\traits\model\order\OrderOper;
use addons\shopro\model\UserOauth;
use app\api\model\ArticleM;
use app\api\model\OrderM;
use app\api\service\ZiS;
use app\common\controller\Api;
use app\common\model\Share;
use think\Validate;
use Throwable;

class OrderPay extends Api
{
    use OrderOper;

    protected $noNeedLogin = 'callback';
    protected $noNeedRight = 'index';

    public function index()
    {
        checkEnv('yansongda');
        $validate = new Validate();
        if (!$validate->check(input(), [
            'xing' => 'require',
            'ming' => 'require',
        ])) {
            $this->error($validate->getError());
        }
        $xing   = input('xing');
        $ming   = input('ming');
        $user   = $this->auth->getUser();
        $openid = UserOauth::where([
            'user_id'  => $user['id'],
            'provider' => 'Wechat',
            'platform' => 'wxMiniProgram'
        ])->value('openid');
        if (!$openid) {
            $this->error('error openid');
        }
        $xing_arr = ZiS::split($xing);
        $ming_arr = ZiS::split($ming);
        if (count($xing_arr) > 2 || count($ming_arr) > 2) {
            $this->error('姓或名最多只能两个字');
        }
        try {
            $info = ZiS::translate($xing_arr, $ming_arr, true);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }
        $order = OrderM::create([
            'payed_is'  => OrderM::PAYED_IS_NO,
            'user_id'   => $user['id'],
            'sn'        => self::getSn($user['id']),
            'xing'      => $xing,
            'ming'      => $ming,
            'info'      => json_encode($info),
            'content'   => '',
            'total_fee' => config('site.ddgm')
        ]);

        $params = (new PayService(
            'wechat',
            'wxMiniProgram',
            $this->request->root(true) . '/api/zi.order_pay/callback'
        ))->create([
            'order_id'     => $order['id'],
            'out_trade_no' => $order['sn'],
            'total_fee'    => config('site.ddgm'),
            'body'         => '单次购买测字',
            'openid'       => $openid,
        ]);
        $this->success('', [
            'params'   => $params,
            'order_id' => $order['id'],
        ]);
    }

    public function callback()
    {
        $pay = new PayService('wechat', 'wxMiniProgram');
        return $pay->notify(function ($data, $pay = null) {
            if ($data['result_code'] != 'SUCCESS' || $data['return_code'] != 'SUCCESS') {
                return false;
            }
            OrderM::where('sn', $data['out_trade_no'])->update([
                'payed_is' => OrderM::PAYED_IS_YES,
            ]);
            //分销
            $order = OrderM::where('sn', $data['out_trade_no'])->find();
            $share = new Share();
            $share->add_goods_range($order['id'], $order['user_id'],true,1);
            // 分配 15% 收益
            $share->add_distribution($order['id'],1);
            return $pay->success()->send();
        });
    }
}
